//
//  textView.m
//  试验品
//
//  Created by romana on 16/5/19.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "textView.h"
#import "AFNetworking.h"
#import "firstModel.h"
#import "SecondModel.h"
#import "smallCollectionViewCell.h"
#import "NXFlowLayout.h"

#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height


@interface textView ()<UICollectionViewDelegate,UICollectionViewDataSource,NXFlowLayoutDelegate>

@property(nonatomic,strong)NSMutableArray *myArray;
@property(nonatomic,strong)UICollectionView *myCollection;
@property(nonatomic,strong)UIPageControl *myPage;
@property(nonatomic,strong)NSMutableArray *myArrayTwo;


@end

@implementation textView

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self doIt];
    }
    
    return self;
}


-(void)doIt{
    
    self.myArray = [NSMutableArray array];
    [self getData];
    [self read];
    NXFlowLayout *flowLayout = [[NXFlowLayout alloc] init];
//    flowLayout.itemSize = CGSizeMake(375 / 2, 90);
//    flowLayout.minimumInteritemSpacing = 10;
//    flowLayout.minimumLineSpacing = 10;
//    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    flowLayout.sectionInset = UIEdgeInsetsMake(0, 5, 30, 5);
    
    flowLayout.ytDelegate = self;
    flowLayout.edgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    flowLayout.colum = 2; // 列数
    flowLayout.rowSpace = 10;
    flowLayout.columSpace = 10;
    flowLayout.row = 3;// 行数
    
    
    

    NSLog(@"====%@",NSStringFromCGRect(self.bounds));
    self.myCollection = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout];
    self.myCollection.pagingEnabled = YES;
   
    self.myCollection.showsHorizontalScrollIndicator = NO;
    self.myCollection.backgroundColor = [UIColor clearColor];
    [self addSubview:self.myCollection];
    
    [self.myCollection registerClass:[smallCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    
    self.myCollection.delegate = self;
    self.myCollection.dataSource = self;
    
    
    
    self.myPage = [[UIPageControl alloc] initWithFrame:CGRectMake(180, 293, 50, 30)];
    self.myPage.backgroundColor = [UIColor blackColor];
    [self addSubview:self.myPage];
    
    self.myPage.numberOfPages = 2;
    self.myPage.alpha = 0.6;
    self.myPage.backgroundColor = [UIColor whiteColor];
    //选中圆点颜色
    self.myPage.currentPageIndicatorTintColor = [UIColor grayColor];
    //未选中圆点颜色
    self.myPage.pageIndicatorTintColor = [UIColor redColor];

    
    
  //  CGRect frame = self.myCollection.frame;
  //  frame.origin.x = 90;

    
}

-(CGFloat)NXFlowLayout:(NXFlowLayout *)flowLayout heightForItemAtIndexPath:(NSIndexPath *)indexPath width:(CGFloat)width{

    return (self.frame.size.height - 40) / 3;


}



//-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//    
////    for (UIScrollView *small in scrollView.subviews) {
////        if ([small isKindOfClass:[UIScrollView class]]) {
////            small.zoomScale = 1.0;
////        }
////    }
//    self.myPage.currentPage = ((self.myCollection.contentOffset.x) - WIDTH) / WIDTH;
//    
//
//}
-(void)read{
    
    self.myArrayTwo = [NSMutableArray array];
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *eventArrayPath = [path stringByAppendingPathComponent:@"TextVieweventArray.txt"];
    NSDictionary *dataDic = [NSDictionary dictionaryWithContentsOfFile:eventArrayPath];

    firstModel *fModel = [[firstModel alloc] init];
    [fModel setValuesForKeysWithDictionary:dataDic];
    
    for (SecondModel *model in fModel.category_list) {
        [self.myArrayTwo addObject:model];
    }


}

-(void)getData{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://open3.bantangapp.com/community/post/index?app_id=com.jzyd.BanTang&app_installtime=1463304161&app_versions=5.7&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&device_token=fd0750748f097a5cce96d0c3f3754ec0c1f23f3ec6288b2c38852fa23e4c8584&oauth_token=a5261fe1f00aaffeb0028f2a7679bf4c&os_versions=9.2.1&screensize=640&track_device_info=iPhone5%2C2&track_deviceid=C520C08B-468B-4B35-A85C-CB045C001E7B&track_user_id=2143663&v=12%20HTTP/1.1" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableDictionary *bigDic = responseObject;
        NSMutableDictionary *dataDic = [bigDic objectForKey:@"data"];
        
        //缓存
        NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
        NSString *eventArrayPath = [path stringByAppendingPathComponent:@"TextVieweventArray.txt"];
        //(2)将字典写入文件
        [dataDic writeToFile:eventArrayPath atomically:YES];
        
        
        firstModel *fModel = [[firstModel alloc] init];
        [fModel setValuesForKeysWithDictionary:dataDic];
        
        for (SecondModel *model in fModel.category_list) {
            [self.myArray addObject:model];
        }
      //  NSLog(@"%@",self.myArray);
        [self.myCollection reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];


}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    if (self.myArray.count == 0) {
        return  self.myArrayTwo.count;
    } else {
        return self.myArray.count;
    }

}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    smallCollectionViewCell *ITEM = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    ITEM.backgroundColor = [UIColor magentaColor];

    if (self.myArray.count == 0) {
        ITEM.seModel = [self.myArrayTwo objectAtIndex:indexPath.item];

    } else {
        ITEM.seModel = [self.myArray objectAtIndex:indexPath.item];

    }
    
    return  ITEM;

}

//-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//
//    NSLog(@"1123123");
//}








@end
